package com.geo.auth.entity;

import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import lombok.Builder;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;

/**
 * 权限实体类
 * 
 * @author GEO Team
 * @version 1.0
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@TableName("sys_permission")
public class Permission {
    
    /**
     * 权限ID
     */
    @TableId(value = "id", type = IdType.ASSIGN_ID)
    private String id;
    
    /**
     * 权限编码（唯一）
     */
    @NotBlank(message = "权限编码不能为空")
    @Size(max = 100, message = "权限编码长度不能超过100个字符")
    @TableField("permission_code")
    private String permissionCode;
    
    /**
     * 权限名称
     */
    @NotBlank(message = "权限名称不能为空")
    @Size(max = 100, message = "权限名称长度不能超过100个字符")
    @TableField("permission_name")
    private String permissionName;
    
    /**
     * 权限描述
     */
    @TableField("description")
    private String description;
    
    /**
     * 权限类型
     */
    @TableField("permission_type")
    private PermissionType permissionType;
    
    /**
     * 父权限ID
     */
    @TableField("parent_id")
    private String parentId;
    
    /**
     * 权限路径（用于菜单导航）
     */
    @TableField("path")
    private String path;
    
    /**
     * 权限组件（前端组件）
     */
    @TableField("component")
    private String component;
    
    /**
     * 权限图标
     */
    @TableField("icon")
    private String icon;
    
    /**
     * 排序
     */
    @TableField("sort_order")
    private Integer sortOrder;
    
    /**
     * 权限状态
     */
    @TableField("status")
    private PermissionStatus status;
    
    /**
     * 是否显示在菜单中
     */
    @TableField("is_visible")
    private Boolean isVisible;
    
    /**
     * 是否缓存页面
     */
    @TableField("is_cache")
    private Boolean isCache;
    
    /**
     * 是否为外链
     */
    @TableField("is_external")
    private Boolean isExternal;
    
    /**
     * HTTP方法
     */
    @TableField("http_method")
    private String httpMethod;
    
    /**
     * API路径
     */
    @TableField("api_path")
    private String apiPath;
    
    /**
     * 备注
     */
    @TableField("remark")
    private String remark;
    
    /**
     * 创建时间
     */
    @TableField(value = "created_at", fill = FieldFill.INSERT)
    private LocalDateTime createdAt;
    
    /**
     * 更新时间
     */
    @TableField(value = "updated_at", fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updatedAt;
    
    /**
     * 创建者ID
     */
    @TableField("created_by")
    private String createdBy;
    
    /**
     * 更新者ID
     */
    @TableField("updated_by")
    private String updatedBy;
    
    /**
     * 逻辑删除标志
     */
    @TableLogic
    @TableField("deleted")
    private Integer deleted;
    
    /**
     * 权限类型枚举
     */
    public enum PermissionType {
        MENU("menu", "菜单"),
        BUTTON("button", "按钮"),
        API("api", "接口"),
        DATA("data", "数据");
        
        private final String code;
        private final String description;
        
        PermissionType(String code, String description) {
            this.code = code;
            this.description = description;
        }
        
        public String getCode() {
            return code;
        }
        
        public String getDescription() {
            return description;
        }
        
        public static PermissionType fromCode(String code) {
            for (PermissionType type : values()) {
                if (type.code.equals(code)) {
                    return type;
                }
            }
            return MENU;
        }
    }
    
    /**
     * 权限状态枚举
     */
    public enum PermissionStatus {
        ACTIVE(1, "启用"),
        INACTIVE(0, "禁用");
        
        private final Integer code;
        private final String description;
        
        PermissionStatus(Integer code, String description) {
            this.code = code;
            this.description = description;
        }
        
        public Integer getCode() {
            return code;
        }
        
        public String getDescription() {
            return description;
        }
        
        public static PermissionStatus fromCode(Integer code) {
            for (PermissionStatus status : values()) {
                if (status.code.equals(code)) {
                    return status;
                }
            }
            return INACTIVE;
        }
    }
    
    /**
     * 检查权限是否可用
     * @return 是否可用
     */
    public boolean isAvailable() {
        return status == PermissionStatus.ACTIVE;
    }
    
    /**
     * 检查是否为菜单权限
     * @return 是否为菜单权限
     */
    public boolean isMenuPermission() {
        return permissionType == PermissionType.MENU;
    }
    
    /**
     * 检查是否为按钮权限
     * @return 是否为按钮权限
     */
    public boolean isButtonPermission() {
        return permissionType == PermissionType.BUTTON;
    }
    
    /**
     * 检查是否为API权限
     * @return 是否为API权限
     */
    public boolean isApiPermission() {
        return permissionType == PermissionType.API;
    }
    
    /**
     * 检查是否为数据权限
     * @return 是否为数据权限
     */
    public boolean isDataPermission() {
        return permissionType == PermissionType.DATA;
    }
    
    /**
     * 检查是否为顶级权限
     * @return 是否为顶级权限
     */
    public boolean isTopLevel() {
        return parentId == null || "0".equals(parentId);
    }
    
    // 手动添加缺失的getter方法
    public String getPermissionCode() {
        return permissionCode;
    }
}